#R 因子

# 创建一个vector
data <- c("East","West","East","North","East","West","West","West","East","South");
print(is.factor( data ));

# 应用factor函数
factor_data <- factor(data);
print( factor_data )
print( is.factor(factor_data ));


#数据帧因子
height <- c(132,151,162,168,147,122,133);
weight <- c(48,59,66,53,67,52,53);
gender <- c("male","male","female","female","male","female","male");

#创建一个data frame
input_data <- data.frame(height,weight,gender);
print( input_data );

#测试其中一列是否为factor
print( is.factor( input_data$gender));
print( is.factor( input_data$height));

#更改级别顺序
new_order_data <- factor( factor_data,levels=c("East","West","North"));
print( new_order_data )

#生成级别因子 gl(n,k,length=n*k,labels=1:n,ordered=FALSE);
# n 正整数 表示因子水平个数
# k 正整数 表示因子每个重复次数
# length 正整数 表示因子向量长度
# labels 表示因子水平名称 默认值 1:n
# ordered 逻辑变量 表示因子水平是否有次序 默认为FALSE

v <- gl(3,4,labels=c("Tampa","Seattle","Boston"));
print( v )

x <- gl(3, 8, labels = c("Control", "Treat","Disconnect"))
print( x )
